package com.example.diary

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.ArrayAdapter
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_main.view.*
import kotlinx.android.synthetic.main.array_adapter.*


class MainActivity : AppCompatActivity(){
    private var datas = ArrayList<String>()
    private var adapter:DemoAdapter? = null

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        add.setOnClickListener {
            val intent = Intent(this, EditActivity::class.java)
            startActivity(intent)
        }
        val dbHelper =MyDatabaseHelper(this,"diary.db",1)
        dbHelper.writableDatabase
        val db=dbHelper.writableDatabase

        val cursor = db.query("DiaryData", null, null, null, null, null, null)
        if (cursor.moveToFirst()) {
            do {
                // 遍历Cursor对象，取出数据并打印
                val title = cursor.getString(cursor.getColumnIndex("title"))
                Log.d("MainActivity", "title is $title")
                datas.add(title)
            } while (cursor.moveToNext())
        }
        cursor.close()


        val adapter=ArrayAdapter<String>(this,R.layout.array_adapter,datas)
        list.adapter=adapter
        list.setOnItemClickListener { parent,view,position,id ->
            Toast.makeText(this,"打开"+datas[position].toString()+"日记",Toast.LENGTH_SHORT).show()
            val intent = Intent(this,Detail::class.java)
            intent.putExtra("标题",datas[position].toString())
            startActivity(intent)

        }

    }

}
